import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class CapturePictureHandle {

    /**
     * 截图
     */
    public  byte[] capturePicture()  {

        // 创建一个默认工具
        Toolkit tool = Toolkit.getDefaultToolkit();
        int x = tool.getScreenSize().width;
        int y = tool.getScreenSize().height;
        int x1 = x/3;
        int y1 = y/3*2;
        int w = x/3;
        int h = y/5;
        Rectangle rectangle = new Rectangle(x1,y1,w,h);
        BufferedImage screenCapture = RobotUtil.ROBOT.createScreenCapture(rectangle);

        //检验下图, 打包是关掉
        //todo
        try {
            ImageIO.write(screenCapture, "png", new File("test"+System.currentTimeMillis()+".png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        ByteArrayOutputStream out =new ByteArrayOutputStream();
        byte[] barray = new byte[0];
        try {
            ImageIO.write(screenCapture,"png",out);//png 为要保存的图片格式
             barray = out.toByteArray();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return barray;
    };

    public static void main(String[] args) {
            byte[] bytes = new CapturePictureHandle().capturePicture();
            System.out.println(bytes.toString());
    }
}
